package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Goods;
import com.example.demo.entity.UserAddress;
import com.example.demo.entity.UserAward;
import com.example.demo.mapper.AwardMapper;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.service.AwardService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AwardServiceImpl implements AwardService {
    @Resource
    private AwardMapper awardMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public Integer addUserAward(String openId, Integer goodId, Integer activityId, Integer userAddressId) {
        Integer businessId = goodsMapper.getBusinessIdByGoodId(goodId);
        Date date = new Date();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        String h_updateTime = sim.format(date);
        UUID uuid=UUID.randomUUID();
        String uuidStr=uuid.toString();
        UserAward userAward = new UserAward(uuidStr, openId, businessId, goodId, h_updateTime, "0", activityId, userAddressId, "null","null","null");
        try {
            return awardMapper.insert(userAward);
        }catch(Exception e){
            return 0;
        }
    }

    public List<Goods> getUserAwardList(String openId){
        QueryWrapper<UserAward> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id",openId);
        List<UserAward> res = awardMapper.selectList(queryWrapper);
        List<Goods> result=new ArrayList<>();
        for(UserAward award:res){
            QueryWrapper<Goods> goodsQueryWrapper=new QueryWrapper<>();
            goodsQueryWrapper.eq("good_id",award.getGoodId());
            Goods good=goodsMapper.selectOne(goodsQueryWrapper);
            good.setOrderId(award.getOrderId());
            good.setShipStatus(award.getShipStatus());
            result.add(good);
        }
        return result;
    }


}
